/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "los_swtmr.h"
#include "time.h"
#include "los_sys.h"
#include "los_api_timer.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#include "los_swtmr.ph"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_timercount3 = 0;
static UINT32 g_timernum = 0;

#define CHECK_SWTMRID(usSwTmrID, uvIntSave, usSwTmrCBID, pstSwtmr)\
{\
    if (usSwTmrID >= OS_SWTMR_MAX_TIMERID)\
   {\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
   uvIntSave = LOS_IntLock();\
   usSwTmrCBID = usSwTmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;\
   pstSwtmr = m_pstSwtmrCBArray + usSwTmrCBID;\
   if (pstSwtmr->usTimerID != usSwTmrID)\
   {\
       LOS_IntRestore(uvIntSave);\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
}
static VOID Timer2_Callback(UINT32 arg)
{
    UINT32 uwRet = LOS_OK;
    UINT32 tick_last2;

    g_timercount3= (UINT32)LOS_TickCountGet();
    
    dprintf("\r\n4. Timer2_Callback entry num=%d,g_timercount3=%d .", g_timernum, g_timercount3);
    g_timernum++;
    // uwRet = LOS_InspectStatusSetByID(LOS_INSPECT_TIMER, LOS_INSPECT_STU_SUCCESS);
    // if (LOS_OK != uwRet)
    // {
    //     dprintf("Set Inspect Status Err .\r\n");
    // }
}

UINT32 Example_TimerStartAndStopEntry(VOID)
{
    UINT32 TempData = 0;
    UINT16 id2;// timer id
    UINT32 uwRet = LOS_OK;
    SWTMR_CTRL_S  *pstSwtmr;
    UINTPTR  uvIntSave;
    UINT16 usSwTmrCBID;
    UINT32 Temp_count;
    dprintf("\r\n--------4.11 Timer period start and stop test!--------\r\n");


#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1);
#endif
    if(LOS_OK != uwRet)
    {
        dprintf("create Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n1. create Timer2 success! uwInterval is 500 ms.\r\n");
    }

    uwRet = LOS_SwtmrStart(id2);
    CHECK_SWTMRID(id2, uvIntSave, usSwTmrCBID, pstSwtmr);
    dprintf("\r\n1. pstSwtmr.uwCount is %d.\r\n",(*pstSwtmr).uwCount);
    dprintf("\r\n2. send 0x11 to start Timer,0x22 to stop Timer,0x55 to end test.\r\n");
    while(TempData!=0x55)
    {
        TempData=(USART0->RXDATA);
        if(TempData& USART_RXDATA_EMPTY)
        {
            TempData=0;
        }
        else
        {
            TempData=(TempData & 0xFF);
        }
        if(TempData==0x11)
        {
            uwRet = LOS_SwtmrStart(id2);
            if (LOS_OK != uwRet)
            {
                dprintf("start Timer2 failed .\r\n");
            }
            else
            {
                dprintf("\r\n2. start Timer2 success!\r\n");
                dprintf("\r\n2. pstSwtmr.uwCount start is %d.\r\n",(*pstSwtmr).uwCount);
            }
            TempData=0;
        }
        else if(TempData==0x22)
        {
            uwRet = LOS_SwtmrStop(id2);
            if (LOS_OK != uwRet)
            {
                dprintf("stop Timer2 failed .\r\n");
            }
            else
            {
                dprintf("\r\n5. stop Timer2 success!\r\n");
                dprintf("\r\n2. pstSwtmr.uwCount stop is %d.\r\n",(*pstSwtmr).uwCount);
            }
            TempData=0;
        }
        LOS_TaskDelay(1);
    }
    uwRet = LOS_SwtmrDelete(id2);
    if (LOS_OK != uwRet)
    {
        dprintf("delete Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n6. delete Timer2 success!\r\n");
    }
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
